#include "GOIProcessor.h"
#include <mastring.h>
#include <mastdlib.h>
#include <matime.h>
#include "conprint.h"
#include "../std.h"
#include "../Utilities/masscanf.h"
#include "../Utilities/trim.h"
#include "../Utilities/SoapHelper.h"

GOIProcessor::GOIProcessor(){}

GOIProcessor::~GOIProcessor(){}

//XmlListener
void GOIProcessor::mtxEncoding(const char* value)
{

}
void GOIProcessor::mtxTagStart(const char* name, int len){

	if (mFirstTag.length()==0){
		mFirstTag.append(name, len);
		mInBody = false;
		mInItem = false;
		return;
	}
	if (strncmp(name, "soap:Body", 9)==0){
		mInBody = true;
		return;
	}
	if (strncmp(name, "SubscriptionInfo", 16) == 0){
		mGOITag == GOI_NULL;
		mInItem = true;
		mCurrentItem.name.clear();
		mCurrentItem.description.clear();
		mCurrentItem.keyword.clear();
		mCurrentItem.serviceid.clear();
		return;
	}else if (strncmp(name, "Name", 4 ) ==0){
		mGOITag = GOI_NAME;
		return;
	}else if (strncmp(name, "Description", 11) ==0){
		mGOITag = GOI_DESCRIPTION;
		return;
	}else if (strncmp(name, "Keyword", 7 ) ==0){
		mGOITag = GOI_KEYWORD;
		return;
	}else if (strncmp(name, "ServiceID", 9 ) ==0){
		mGOITag = GOI_SERVICEID;
		return;
	}
}

void GOIProcessor::mtxTagAttr(const char* attrName, const char* attrValue){

}
void GOIProcessor::mtxTagStartEnd(){
	if (mFirstTag.length()== 0){
		mListener->processingComplete();
	}
}
void GOIProcessor::mtxTagData(const char* data, int len){
	temp.append(data, len);
}
void GOIProcessor::mtxTagEnd(const char* name, int len){
	//trim(temp);
	//parseControlCodes(temp);
	//stripHTML(temp);

	switch(mGOITag){
	case GOI_NULL:
		break;
	case GOI_NAME:
		mCurrentItem.name = temp;
		break;
	case GOI_DESCRIPTION:
		mCurrentItem.description = temp;
		break;
	case GOI_KEYWORD:
		mCurrentItem.keyword = temp;
		break;
	case GOI_SERVICEID:
		mCurrentItem.serviceid = temp;
		break;
	}

	temp.clear();

	mGOITag = GOI_NULL;

	if (strncmp(name, "SubscriptionInfo", 16)==0){
		mInItem = false;
		if (mListener != NULL){
			mListener->itemReceived(mCurrentItem);
		}
	}

	if(strcmp(name, "soap:Body") == 0) {
			mInBody = false;
		}

	if (mFirstTag == name){
		mFirstTag.clear();
		mListener->processingComplete();
	}
}

void GOIProcessor::mtxParseError(){

}
void GOIProcessor::mtxEmptyTagEnd(){

}
unsigned char GOIProcessor::mtxUnicodeCharacter(int unicode){
	char c = getUnicodeCharacter(unicode);
	return c;
}

//Listener
void GOIProcessor::setGOIListener(GOIListener* listener){
	mListener = listener;
}

void GOIProcessor::addRequest(String& url, GOIListener* requester){
	if (APPCONTROLLER->getConnection()->isBusy()) return;

	String sSoap = GOI_GetList();

	//lprintfln(sSoap.c_str());

	int sSoapLen = sSoap.length();
	String sSoapLenStr = integerToString(sSoapLen);
	String sUserinfo = "smartfaceuserinfo";
	String sUserinfovalue = "serviceID=100&uid=" + APPCONTROLLER->getUserInfoValue() + "&";
	String sLength = "Content-Length";
	String sType = "Content-Type";
	String sTypeValue = "text/xml;charset=UTF-8";
	APPCONTROLLER->getConnection()->clearHeaders();
	APPCONTROLLER->getConnection()->setHeader(sLength, sSoapLenStr);
	APPCONTROLLER->getConnection()->setHeader(sType, sTypeValue);
	APPCONTROLLER->getConnection()->setHeader(sUserinfo, sUserinfovalue);
	APPCONTROLLER->getConnection()->setData(sSoap);
	APPCONTROLLER->getConnection()->setXmlListener(this);

	APPCONTROLLER->getConnection()->setURL(url.c_str());
	mListener = requester;
	lprintfln("begin GOI download");
	APPCONTROLLER->getConnection()->beginDownloading();
}
